

#include <iostream>
using namespace std;

#include <string>
class Sln {
public:
    int myAtoi(string str) 
    {

        for (int i = 0; i < str.size(); ++i)
        {
            if (isblank(str[i]))
                continue;

            if (!(isdigit(str[i]) || str[i] == '+' || str[i] == '-'))
                return 0;

            int j = i, flag = 1;
            if (str[j] == '-' || str[j] == '+')
            {
                flag = str[j] == '+' ? 1 : -1;
                ++j;
            }

            string s;
            int brk = 0;
            while (j < str.size())
            {
                if (brk)
                    break;

                if (isdigit(str[j]))
                {
                    s += str[j];
                }
                else
                {
                    brk = 1;
                }

                ++j;
            }

               
            if (s[0] == '0')
            {
                int pos = 0;
                for (int i = 1; i < s.size(); ++i)
                {

                    if (s[i] == s[i - 1] && s[i] == '0')
                    {
                        pos = i + 1;

                        if (pos > s.size() - 1)
                        {
                            s.clear();
                            break;
                        }
                    }
                    else
                    {
                        s = s.substr(pos);
                        break;
                    }
                }
            }

            if (s.size())
            {
                if (s.size() == 10 && s >= to_string(INT_MAX))
                {
                    if (flag == 1)
                    {
                        return INT_MAX;
                    }
                    else
                    {
                        if (s >= to_string(INT_MIN).substr(1))
                            return INT_MIN;
                        else
                            return flag * stoi(s);
                    }         
                }
                else if (s.size() > 10)
                {
                    if (flag == 1)
                        return INT_MAX;
                    else
                        return INT_MIN;
                }
                else
                {
                    return flag * stoi(s);
                } 
            }
            else
            {
                return 0;
            }
        }

        return 0;
    }
};

class Sln2 {
public:
    int myAtoi(string str) {
        for (int i = 0; i < str.size(); ++i)
        {
            if (isblank(str[i]))
                continue;

            if (str[i] == '+' || str[i] == '-' || isdigit(str[i]))
            {
                int flag = 1;
                if (str[i] == '+' || str[i] == '-')
                {
                    flag = str[i] == '+' ? 1 : -1;
                    ++i;
                }

                long ret = 0;
                while (i < str.size())
                {
                    if (isdigit(str[i]))
                    {
                        ret = ret * 10 + (str[i] - '0');
                        if (ret > INT_MAX || ret < INT_MIN)
                        {
                            return flag == 1 ? INT_MAX : INT_MIN;
                        }
                        ++i;
                    }
                    else
                        break;
                }

                return flag * ret;
            }
            else
            {
                return 0;
            }
        }

        return 0;
    }
};

class Sln3 {
public:
    int myAtoi(string str) {
        int i = 0;
        if (0 == str.size())
            return 0;

        while (isblank(str[i]))
        {
            ++i;
            if (i >= str.size())
                return 0;
        }

        int flag = 1;
        if (str[i] == '-')
            flag = -1;
        if (str[i] == '+' || str[i] == '-')
            ++i;

        long ret = 0;
        for (; i < str.size() && isdigit(str[i]); ++i)
        {
            ret = ret * 10 + str[i] - '0';
            if (ret > INT_MAX || ret < INT_MIN)
                return flag == 1 ? INT_MAX : INT_MIN;
        }

        return flag * ret;
    }
};

class Sln4 {
public:
    int myAtoi(string str) {
        int i = 0;
        if (0 == str.size())
            return 0;

        while (isblank(str[i]))
        {
            if (++i >= str.size())
                return 0;
        }

        int flag = 1;
        if (str[i] == '-')
            flag = -flag;
        if (str[i] == '+' || str[i] == '-')
            ++i;

        int ret = 0, bnd = INT_MAX / 10;
        while (isdigit(str[i]) && i < str.size())
        {
            if (ret > bnd || (ret == bnd && str[i] > '7'))
                return flag == 1 ? INT_MAX : INT_MIN;

            ret = ret * 10 + (str[i++] - '0');
        }

        return flag * ret;
    }
};

int main()
{
    //cout << Sln().myAtoi(string("+1"));
    //cout << Sln().myAtoi(string("-2147483648"));
    //cout << Sln().myAtoi(string("-2147483647"));
    //cout << Sln().myAtoi(string("20000000000000000000"));
    //cout << Sln().myAtoi(string("  0000000000012345678"));
    //cout << Sln().myAtoi(string("     +004500"));
   
    //cout << to_string(INT_MAX);


    //cout << Sln2().myAtoi(string("42"));
    //cout << Sln3().myAtoi(string("  0000000000012345678"));
    cout << Sln4().myAtoi(string("-2147483648"));


	return 0;
}